************************************************************************************************
* Purpose: merge sample with info about screening for maternal mental health and coverage rates at municipality level
* Using NOVAX nurse data from mental health module
******************************************************************************************

do "D:\Data\workdata\708296\Project - Mother groups and mental health\Do\global.do" 

use "$raw\efterfodselsreaktion", clear
gen cprm=Svarperson_CPR
gen cprf=Svarperson_CPR
gen cprb=Relation_til_svarperson_CPR 
gen gender=(Svarperson_Køn=="Pige")
drop Antal* Svarperson_Køn
gen year=year(Registreringsdato_Dato)
rename Registreringsdato_Dato Dato

gen help=substr(Svarperson_CPR, -12,1) // drop few missing cpr spells
drop if help==""
drop help

sort cprm Dato Kommune_Kommunekode Relation_til_svarperson_CPR

replace Spørgeskema___total_score="." if Spørgeskema___total_score=="Intet svar tilknyttet"
destring Spørgeskema___total_score, replace
rename Spørgeskema___total_score score

 *replace missing child pnr with child pnr from later tests //
gen help=substr(cprb, -12,1)
sort cprm Dato Kommune_Kommunekode score Relation_til_svarperson_CPR
replace cprb=cprb[_n+1] if cprm[_n+1]==cprm & help==""
drop help

//Doesn't distinguish between pregnant and after- do it by comparing date of birth and date of test
gen score_epds=.
replace score_epds= score if Spørgeskema=="EPDS gravide"
replace score_epds= score if Spørgeskema=="EPDS nybagt"
replace score_epds= score if Spørgeskema=="Edinburgh"
replace score_epds= score if Spørgeskema=="Edinburgh gravide"
replace score_epds= score if Spørgeskema=="Edinburgh graviditet"
replace score_epds= score if Spørgeskema=="Edinburgh Gravid"

gen score_gotland=.
replace score_gotland=score if Spørgeskema=="Gotland"
replace score_gotland=score if Spørgeskema=="Gotland 2018"
replace score_gotland=score if Spørgeskema=="Gotland Gravid"
replace score_gotland=score if Spørgeskema=="Gotland spørgeskema"
replace score_gotland=score if Spørgeskema=="Gotland spÃ¸rgeskema"
replace score_gotland=score if Spørgeskema=="Gotland til mÃ¦nd"
replace score_gotland=score if Spørgeskema=="Gotland-skalaen"
drop Spørgeskema

bys Svarperson_CPR Dato: egen score_g=min(score_gotland) 
bys Svarperson_CPR Dato: egen score_e=min(score_epds) 

sort Svarperson_CPR Dato Kommune_Kommunekode score Relation_til_svarperson_CPR
by Svarperson_CPR Dato: keep if _n==1
drop score_epds score_gotland score Relation_til_svarperson_CPR
save "$work\temp_efterf", replace 



********************************************************************************
* Kids not in registrations. We locate children - that will be appended to temp_efterf.dta 
********************************************************************************
gen help=substr(cprb, -12,1) // a small set of kids 
tab help,miss
keep if help==""
dis _N
drop help
save "$work\missing_childpnr_efterf.dta",replace 

use "$work\missing_childpnr_efterf.dta", clear // mother registered
tab gender
drop if gender==0
drop cprf
sort cprm Dato Kommune_Kommunekode score_e cprb
by cprm: gen id = _n
reshape wide Dato Kommune_Kommunekode cprb year score_e score_g, i(cprm) j(id)
rename cprm id_mother
merge 1:m id_mother using "$work\mfr_kids.dta" , keep(3) nogen
order id_mother mfr_dob Dato1 
format mfr_dob %td
foreach x in 1 2 {
gen between`x' = abs(Dato`x'-mfr_dob)
}

rename  id_mother cprm
rename mfr_dob dob

* finds relevant kid and mother using test date
foreach x in 1 2 {
	sort cprm Dato1 Kommune_Kommunekode1 score_e1 cprb1
by cprm: egen mindsteforskel`x'=min(between`x')
replace between`x'=. if between`x'!=mindsteforskel`x' 
}
order cprm dob Dato1 between1
foreach x in 1 2 {
replace year`x'=. if between`x'==.
replace score_g`x'=. if between`x'==.
replace score_e`x'=. if between`x'==.
replace Dato`x'=. if between`x'==.
}

replace Dato1=Dato2 if Dato1==. & Dato2!=.
foreach var in score_g score_e between {
replace `var'1=`var'2 if Dato1==Dato2
replace `var'2=. if Dato1==Dato2
}
replace Dato2=. if Dato1==Dato2
drop if  Dato1==. & Dato2==.
keep pnr cprm Dato* Kommune_Kommunekode* year* score_g* score_e*

rename pnr cprb
gen imputed=1

* reshape long before merging back
reshape long  Dato Kommune_Kommunekode year score_g score_e ,i(cprb) j(help)
drop if score_g == . & score_e ==. 
drop help
gen gender=1
compress
save "$work\childpnr_efterf_mor.dta", replace 

********************************************************************************
* now append all togther
********************************************************************************

use "$work\temp_efterf", clear
gen help=substr(cprb, -12,1)  
drop if help==""
drop help

* append the spells now found the kids pnr! if prenatal test same obs already in data
append using "$work\childpnr_efterf_mor.dta"
drop imputed

gen pnr=cprb
merge m:1 pnr using "$work\mfr_kids.dta"  
keep if _merge==3
drop _merge

* mark the first test after birth:
gen test_postnatal=0 
replace test_postnatal=1 if Dato>mfr_dob
label var test_postnatal "Postnatal test"

sort pnr Dato 
gen age_test=Dato-mfr_dob
label var age_test "Child's age at test" // some of the tests must be about other kids - too long distance

* drop tests that have weird timing. maybe those are on other kids - very few obs
drop if age_test<-280 
drop if age_tes >530

gen prenataltest1=1 if age_test<=0
bys pnr: egen prenataltest=min(prenataltest1)
drop prenataltest1

* make sure that test 1 in the wide format is a postnatal test
drop if age_test<=0

* moms - only do score EPDS,not Gothland
drop score_g
keep if gender==1
drop Svarperson_CPR gender cprf
sort cprb cprm Dato score_e
by cprb : gen help=_n
reshape wide Dato test_postnatal age_test Kommune_Kommunekode cprm  year  score_e, i(cprb) j(help)
rename cprm1 cprm 
order cprm*
drop cprm2-cprm5  
save "$work\efterfodsel_mor.dta", replace 

************************************************************************************
* Coverage: Munics and years consistently using epds
************************************************************************************

use "$work\efterfodsel_mor.dta", clear

gen tested = 1

* merge med mfr sample
merge 1:1 pnr using "$work\mfr_kids"
drop _merge

replace tested = 0 if tested !=1

sort mfr_muni mfr_yob
collapse (mean) tested, by(mfr_muni mfr_yob)  // yearly share of tested

gen treated = mfr_yob if tested >=0.5
bysort mfr_muni: egen mintreat = min(treated)
gen years = 1 if mfr_yob>=mintreat
replace years=0 if mfr_yob < mintreat
keep if years == 1
keep mfr_muni mfr_yob treated tested
save "$work\munics_tested", replace